package net.pimpas.web.jsf.renderers.tree.html;

import java.io.IOException;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.render.Renderer;

import net.pimpas.logging.serviceprovider.LoggingServiceProvider;
import net.pimpas.logging.serviceprovider.LoggingServiceProviderFactoryImpl;
import net.pimpas.resourcebundle.serviceprovider.ResourceBundleServiceProvider;
import net.pimpas.resourcebundle.serviceprovider.ResourceBundleServiceProviderFactoryImpl;
import net.pimpas.web.jsf.components.span.UISpan;
import net.pimpas.web.jsf.components.tree.TreeNode;
import net.pimpas.web.jsf.components.tree.UITree;
import net.pimpas.web.jsf.renderers.RendererBase;
import net.pimpas.web.jsf.util.ComponentUtils;

public class HtmlTree extends RendererBase {
	
	private static final LoggingServiceProvider logger = 
		LoggingServiceProviderFactoryImpl.getLoggingServiceProvider(HtmlTree.class);
	private static final ResourceBundleServiceProvider bundle = 
		ResourceBundleServiceProviderFactoryImpl.getResourceBundleServiceProvider();
	
	@Override
	@SuppressWarnings("unchecked")
	public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
		super.initResourceManager(context, component);
		if(!component.isRendered()) return;
		if(!ComponentUtils.isCorrectComponentType(component, UITree.class)) return;
		
		UISpan span = (UISpan) context.getApplication().createComponent(UISpan.COMPONENT_TYPE);
		span.setStyleClass(UITree.DEFAULT_COMPONENT_STYLE);
		component.getAttributes().put("span", span);
		
		Renderer render = 
			context.getRenderKit().getRenderer(UISpan.COMPONENT_FAMILY, UISpan.COMPONENT_RENDER_TYPE);
		
		render.encodeBegin(context, span);
		
		// process value
		UITree tree = (UITree) component;
		List<TreeNode> nodes = (List<TreeNode>) tree.getValueBinding("value").getValue(context);
		
		
	}
	
	@Override
	public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
		if(!component.isRendered()) return;
		if(!ComponentUtils.isCorrectComponentType(component, UITree.class)) return;
		Renderer render = 
			context.getRenderKit().getRenderer(UISpan.COMPONENT_FAMILY, UISpan.COMPONENT_RENDER_TYPE);
		
		render.encodeEnd(context, (UIComponent) component.getAttributes().remove("span"));
	}
}